<?php
 
class Alcance_IndexController extends Zend_Controller_Action {

    public function init() {
        /*Initialize action controller here */
    }

    public function myalcanceAction() {
        $row = new Alcance_Model_DbTable_Alcanse();
        $auth = Zend_Auth::getInstance();
        $user = $auth->getIdentity()->id;
        $row = $row->fetchAll("id_user_create = '$user'");
        $this->view->row = $row;
    }

    public function viewalcanceAction() {
        $id = $this->_getParam('id', 0);
        $fin = $this->_getParam('fin', 'none');
        if (!is_numeric($id) or $id <= 0) {
            $this->_helper->FlashMessenger()->setNamespace('error')->addMessage('Solicitud no valida.');
            $this->_helper->redirector->gotoUrlAndExit('/alcance/index/myalcance');
        }
        $alc = new Alcance_Model_DbTable_Alcanse();
        $this->view->alc = $alc->fetchRow("id = '$id'");
        $tarea = new Tarea_Model_DbTable_Tarea();
        $complete = $tarea->fetchAll("id_alcance = " . $this->view->alc['id'] . " and status = '0'")->count();
        $this->view->count_tarea = $complete;
        $this->view->tareas = $tarea->fetchAll("id_alcance = " . $id);

        if ($fin == 'fin') {
            if ($complete > 0) {
                $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('No se puede completar el alcance porque hay taresa pendientes.');
                $this->_helper->redirector->gotoUrlAndExit('/alcance/index/viewalcance/id/' . $id);
            } else {
                $alc->update(array('status' => 1), "id = '$id'");
                $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Alcance completado.');
                $this->_helper->redirector->gotoUrlAndExit('/alcance/index/myalcance');
            }
        }
    }

    public function viewAction() {
        $row = new Alcance_Model_DbTable_Alcanse();
        $row = $row->fetchAll();
        $this->view->row = $row;
    }

    public function deleteAction() {
        $id = $this->_getParam('id', 0);
        $tarea = new Tarea_Model_DbTable_Tarea();
        $complete = $tarea->fetchAll("id_alcance = " . $id . " and status = '0'")->count();
        if ($complete > 0) {
            $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('No se pudo eliminar el alcance porque tiene tare pendientes');
            $this->_helper->redirector->gotoUrlAndExit('/alcance/index/myalcance');
        }
        $alc = new Alcance_Model_DbTable_Alcanse();
        $alc->remove((int) $id);
        $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Alcance eliminado con exito.');
        $this->_helper->redirector->gotoUrlAndExit('/alcance/index/view');
    }

    public function addalcanceAction() {
        $id_orden = $this->_getParam('id', 'none');
        $form = new Alcance_Form_Add();
        if (!is_numeric($id_orden) or $id_orden <= 0) {
            $this->combo = 'h';
        } else {
            $this->view->id = $id_orden;
            $title = new Orden_Model_DbTable_Orden();
            $this->view->title = $title->getTitle($id_orden);
        }
        $elem = $form->getElement('id_orden');
        $orden = new Orden_Model_DbTable_Orden();
        $orden = $orden->getCombo();
        $elem->addMultiOptions($orden);
        // var_dump($orden);
        $form->setAction($this->getRequest()->getRequestUri());
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                unset($formData['submit']);
                $auth = Zend_Auth::getInstance();
                $iduser = $auth->getIdentity()->id;
                $formData['id_user_create'] = $iduser;
             //   $formData['id_orden'] = $id_orden;
                //echo $formData['id_orden'];
                $formData['end_date'] = date('Y-m-d', strtotime($formData['end_date']));
                $formData['create_date'] = date('Y-m-d', strtotime($formData['create_date']));
                $userForm = new Alcance_Model_DbTable_Alcanse();
                $userForm->add($formData);
                $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Alcance adicionada con exito.');
                $this->_helper->redirector->gotoUrlAndExit('/alcance/index/addalcance/id/' . $id_orden);
            } else {
                $form->populate($formData);
                $this->view->form = $form;
            }
        } else {
            $this->view->form = $form;
        }
    }

    public function editAction() {
        $id = $this->_getParam('id', 'none');
        if (!is_numeric($id) or $id <= 0) {
            $this->_helper->FlashMessenger()->setNamespace('error')->addMessage('Ha ocurrido un error en la URL.');
            $this->_helper->redirector->gotoUrlAndExit('/alcance/index/view');
        }
        $form = new Alcance_Form_Add();
        $elem = $form->getElement('id_orden');
        $orden = new Orden_Model_DbTable_Orden();
        $orden = $orden->getCombo();
        $elem->addMultiOptions($orden);
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                unset($formData['submit']);
                $userForm = new Alcance_Model_DbTable_Alcanse();
                $formData['update_date'] = date('Y-m-d');
                $formData['end_date'] = date('Y-m-d', strtotime($formData['end_date']));
                $userForm->edit($formData, $id);
                $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Tarea Modificada con exito.');
                $this->_helper->redirector->gotoUrlAndExit('/alcance/index/view');
            } else {
                $form->populate($formData);
                $this->view->form = $form;
            }
        } else {
            $row = new Tarea_Model_DbTable_Tarea();
            $row = $row->fetchAll("id_alcance=" . (int) $id);
            $this->view->row = $row;
            $formData = new Alcance_Model_DbTable_Alcanse();
            $formData = $formData->fetchRow("id=" . $id)->toArray();
            $formData['end_date'] = date('d-m-Y', strtotime($formData['end_date']));
            $form->populate($formData);
            $this->view->form = $form;
        }
    }

    function es_int($numero) {
        if (preg_match("/^(+|-)?[0-9]+$/", $numero))
            return true;
        else
            return false;
    }

}

?>
